<template>
  <div class="com-container">
    
    <div class="com-chart" ref="stock_ref">
      
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  data () {
    return {
      chartInstance: null,
      allData: null, // 从服务器获取的所有数据
      currentIndex: 0, // 当前显示的数据的页数
      timerId: null, 
    }
  },
  computed: {...mapState(['theme']) },
  watch:{
    theme() {
      console.log("主题切换了");
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕适配
      this.updateChart() // 更新图表的展示
    }
  },
  created() {
   // 在组件创建完毕后, 注册回调函数
    this.$socket.registerCallBack("stockData", this.getData)
  },
  mounted() {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      charName: 'stock',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter)
    clearInterval(this.timerId)
    this.$socket.unRegisterCallBack("stockData")
  },
  methods: {
    // 初始化方法
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, this.theme)
      const initOption = {
        title: {
          text: '▎库存和销量分析',
          left: 20,
          top: 20
        }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取服务器数据方法
    getData(ret) {
      // await this.$http.get()
      // 对allData进行赋值
      // const {data:ret} = await this.$http.get('stock')
      this.allData = ret
      console.log(this.allData);
      this.updateChart()
      this.startInterval()
    },
    // 更新图表
    updateChart() {
      // 中心的坐标数据
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4ff778', '#0ba82c'],
        ['#e5dd45', '#e8b11c'],
        ['#e8821c', '#e55445'],
        ['#5052ee', '#ab6ee5'],
        ['#23e5e5', '#2e72bf']
      ]
      // 处理数据
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1)  * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item,index) => {
        return {
          type: 'pie',
          // radius: [110, 100],
          center: centerArr[index],
          hoverAnimation: false,  // 取消鼠标经过时,圆环变细的动画
          labelLine: {
            show: false    // 隐藏指示线
          }, 
          label: {
            position: 'center',  // 让文字显示在圆环内部
            color: colorArr[index][0]
          },
          data: [
            {
              // 销量数据
              value: item.sales,
              name: item.name + '\n\n' + item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0,1,0,0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              // 库存数据
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })
      const dataOption = {
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      const titleFontSize = this.$refs.stock_ref.offsetWidth / 100 * 3.6
      // 内圆半径
      const innerRadius = titleFontSize * 2.8
      // 外圆半径
      const outterRadius = titleFontSize * 2
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval() {
      if(this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentIndex++;
        if(this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.updateChart()  //更改完currentIndex后,需要更新页面
      }, 3000);
    }
  },
}
</script>
 
<style lang = "less" scoped>
  
</style>